// 应用程序核心功能
class App {
    constructor() {
        this.currentSection = 'home';
        this.sections = document.querySelectorAll('.section');
        this.navItems = document.querySelectorAll('.nav-item');
        this.bottomNavItems = document.querySelectorAll('.bottom-nav .nav-item');
        
        this.initializeEventListeners();
        this.handleHashChange();
    }

    initializeEventListeners() {
        // 监听导航点击事件
        this.navItems.forEach(item => {
            item.addEventListener('click', (e) => {
                e.preventDefault();
                const target = item.getAttribute('href').substring(1);
                this.navigateTo(target);
            });
        });

        // 监听底部导航点击事件
        this.bottomNavItems.forEach(item => {
            item.addEventListener('click', (e) => {
                e.preventDefault();
                const target = item.getAttribute('href').substring(1);
                this.navigateTo(target);
            });
        });

        // 监听 URL hash 变化
        window.addEventListener('hashchange', () => this.handleHashChange());
    }

    navigateTo(section) {
        // 更新 URL hash
        window.location.hash = section;
    }

    handleHashChange() {
        // 从 URL hash 获取目标部分
        const hash = window.location.hash.substring(1) || 'home';
        
        // 更新当前部分
        this.currentSection = hash;
        
        // 更新部分显示状态
        this.sections.forEach(section => {
            if (section.id === hash) {
                section.classList.add('active');
            } else {
                section.classList.remove('active');
            }
        });
        
        // 更新导航项目激活状态
        this.navItems.forEach(item => {
            const target = item.getAttribute('href').substring(1);
            if (target === hash) {
                item.classList.add('active');
            } else {
                item.classList.remove('active');
            }
        });
        
        // 更新底部导航项目激活状态
        this.bottomNavItems.forEach(item => {
            const target = item.getAttribute('href').substring(1);
            if (target === hash) {
                item.classList.add('active');
            } else {
                item.classList.remove('active');
            }
        });
    }

    // 打开认证模态框
    openAuth(type = 'login') {
        const modal = document.getElementById('authModal');
        const tabs = modal.querySelectorAll('.auth-tab');
        const forms = modal.querySelectorAll('.auth-form');
        
        // 显示模态框
        modal.style.display = 'flex';
        
        // 切换到指定的表单
        tabs.forEach(tab => {
            if (tab.dataset.tab === type) {
                tab.classList.add('active');
            } else {
                tab.classList.remove('active');
            }
        });
        
        forms.forEach(form => {
            if (form.id === `${type}Form`) {
                form.classList.add('active');
            } else {
                form.classList.remove('active');
            }
        });

        // 添加关闭按钮事件
        const closeBtn = modal.querySelector('.close-btn');
        closeBtn.onclick = () => {
            modal.style.display = 'none';
        };

        // 点击模态框外部关闭
        window.onclick = (event) => {
            if (event.target === modal) {
                modal.style.display = 'none';
            }
        };

        // 添加表单切换事件
        tabs.forEach(tab => {
            tab.onclick = () => {
                const type = tab.dataset.tab;
                
                // 更新标签激活状态
                tabs.forEach(t => t.classList.remove('active'));
                tab.classList.add('active');
                
                // 更新表单显示
                forms.forEach(form => {
                    if (form.id === `${type}Form`) {
                        form.classList.add('active');
                    } else {
                        form.classList.remove('active');
                    }
                });
            };
        });
    }
}

// 创建应用实例
window.app = new App(); 